package com.readaloud.wicket;


import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.protocol.http.WebSession;

import com.readaloud.hibernate.user.Books;
import com.readaloud.hibernate.user.Comment;

public class ViewBook extends WebPage {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public ViewBook(Books book) {
		add(new ViewBookForm("viewBook", book));
	}

	private class ViewBookForm extends Form<Component> {

		private String name;
		private String author;
		private String title;
		private String publishDate;
		private List<Comment> comment = new ArrayList<Comment>();
		
		private String insertComment;
		private Books book;
		WebSession session = new WebSession(getRequest());
		
		
		public ViewBookForm(String id, Books book) {
			super(id);
			System.out.println("BOOK: "+book);
			this.book = book;
			setDefaultModel(new CompoundPropertyModel<Books>(book));
			add(new Label("name"));
			add(new Label("title"));
			add(new Label("author"));
			add(new Label("publishDate"));
			
			ListView comment = new ListView("commentList", ((List<Comment>)((Books)getDefaultModel().getObject()).getComment())){
				@Override
				protected void populateItem(ListItem item) {
			        Comment comment = (Comment) item.getModelObject();
					item.add(new Label("comment", comment.getCommentDesc()));
					item.add(new Label("readerName", comment.getReader().getUserName()));
				}
			};
	        add(comment);
	        
			add(new TextArea<String>("insertComment", new PropertyModel<String>(this,"insertComment")));
		}

		protected void onSubmit() {

			System.out.println("getInsertComment: "+getInsertComment());
			Books updatedBook = WicketApplication
					.get()
					.getInvertedIndexDao()
					.addComment(
							Integer.parseInt(session.getAttribute("userId")
									.toString()),
							session.getAttribute("userName").toString(),
							this.book, getInsertComment());
            setResponsePage(new ViewBook(updatedBook));
		}

		public String getAuthor() {
			return author;
		}

		public void setAuthor(String author) {
			this.author = author;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getPublishDate() {
			return publishDate;
		}

		public void setPublishDate(String publishDate) {
			this.publishDate = publishDate;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public List<Comment> getComment() {
			return comment;
		}

		public void setComment(List<Comment> comment) {
			this.comment = comment;
		}

		public String getInsertComment() {
			return insertComment;
		}

		public void setInsertComment(String insertComment) {
			this.insertComment = insertComment;
		}

	}

}
